<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Go语言基础之单元测试 | 小狐档案库</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="学习资料">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.0c36f186.css" as="style"><link rel="preload" href="/assets/js/app.7029ddab.js" as="script"><link rel="preload" href="/assets/js/3.ebaa3085.js" as="script"><link rel="preload" href="/assets/js/1.8ce67e8c.js" as="script"><link rel="preload" href="/assets/js/18.fbd494c2.js" as="script"><link rel="prefetch" href="/assets/js/10.15c1f9c8.js"><link rel="prefetch" href="/assets/js/11.dc2fbddf.js"><link rel="prefetch" href="/assets/js/12.26ab1f36.js"><link rel="prefetch" href="/assets/js/13.6e1d4dbc.js"><link rel="prefetch" href="/assets/js/14.0cd48e80.js"><link rel="prefetch" href="/assets/js/15.74a9f029.js"><link rel="prefetch" href="/assets/js/16.24ef3382.js"><link rel="prefetch" href="/assets/js/17.c8a9bd0f.js"><link rel="prefetch" href="/assets/js/19.f9a0df71.js"><link rel="prefetch" href="/assets/js/20.e6d0df38.js"><link rel="prefetch" href="/assets/js/21.932645cc.js"><link rel="prefetch" href="/assets/js/22.d6027dce.js"><link rel="prefetch" href="/assets/js/23.53bce141.js"><link rel="prefetch" href="/assets/js/24.ed3b8b64.js"><link rel="prefetch" href="/assets/js/25.ed88877e.js"><link rel="prefetch" href="/assets/js/26.f5ad24d5.js"><link rel="prefetch" href="/assets/js/27.64ad45b4.js"><link rel="prefetch" href="/assets/js/28.f308f8ab.js"><link rel="prefetch" href="/assets/js/29.4d95742d.js"><link rel="prefetch" href="/assets/js/30.0a867de0.js"><link rel="prefetch" href="/assets/js/31.f12def86.js"><link rel="prefetch" href="/assets/js/32.6b4fd8b7.js"><link rel="prefetch" href="/assets/js/33.4bb2aa9c.js"><link rel="prefetch" href="/assets/js/34.013e731c.js"><link rel="prefetch" href="/assets/js/35.6b263ef8.js"><link rel="prefetch" href="/assets/js/36.06acf3cc.js"><link rel="prefetch" href="/assets/js/37.fe72c29c.js"><link rel="prefetch" href="/assets/js/38.9b4e0bd6.js"><link rel="prefetch" href="/assets/js/39.0cde0408.js"><link rel="prefetch" href="/assets/js/4.9b5650bb.js"><link rel="prefetch" href="/assets/js/40.56bda9a8.js"><link rel="prefetch" href="/assets/js/41.007ae34c.js"><link rel="prefetch" href="/assets/js/42.bdaf414b.js"><link rel="prefetch" href="/assets/js/43.78d32d4e.js"><link rel="prefetch" href="/assets/js/44.a10cf8a5.js"><link rel="prefetch" href="/assets/js/45.19e14721.js"><link rel="prefetch" href="/assets/js/46.57ebcf06.js"><link rel="prefetch" href="/assets/js/47.9a513ffe.js"><link rel="prefetch" href="/assets/js/48.245b165a.js"><link rel="prefetch" href="/assets/js/49.fab43655.js"><link rel="prefetch" href="/assets/js/5.a16ef7f8.js"><link rel="prefetch" href="/assets/js/50.48c191fd.js"><link rel="prefetch" href="/assets/js/51.28a94b0e.js"><link rel="prefetch" href="/assets/js/52.e783fbf9.js"><link rel="prefetch" href="/assets/js/53.2f0e960e.js"><link rel="prefetch" href="/assets/js/54.577ff54e.js"><link rel="prefetch" href="/assets/js/55.4bb99598.js"><link rel="prefetch" href="/assets/js/56.e820e954.js"><link rel="prefetch" href="/assets/js/57.0cea0cfa.js"><link rel="prefetch" href="/assets/js/58.e31cfcd0.js"><link rel="prefetch" href="/assets/js/59.458ab168.js"><link rel="prefetch" href="/assets/js/6.7da424ba.js"><link rel="prefetch" href="/assets/js/60.e9caace2.js"><link rel="prefetch" href="/assets/js/61.0b61afdb.js"><link rel="prefetch" href="/assets/js/62.c240acab.js"><link rel="prefetch" href="/assets/js/63.71cd2a97.js"><link rel="prefetch" href="/assets/js/64.977ea29b.js"><link rel="prefetch" href="/assets/js/65.79b2d62a.js"><link rel="prefetch" href="/assets/js/66.8da69071.js"><link rel="prefetch" href="/assets/js/67.fdc57d7e.js"><link rel="prefetch" href="/assets/js/68.d43eabe9.js"><link rel="prefetch" href="/assets/js/7.6e92edd1.js"><link rel="prefetch" href="/assets/js/8.2389170b.js"><link rel="prefetch" href="/assets/js/9.77841f24.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0c36f186.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小狐档案库</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>学习资料</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小狐档案库" class="logo"> <span class="site-name">小狐档案库</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    小狐
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>58</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>20</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>go语言学习</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>go语言基础学习</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blogs/GO/go/1_go_env.html" class="sidebar-link">Go环境安装配置</a></li><li><a href="/blogs/GO/go/2_go_datatype.html" class="sidebar-link">GO基础-基本数据类型</a></li><li><a href="/blogs/GO/go/3_go_var_const.html" class="sidebar-link">Go基础-变量和常量</a></li><li><a href="/blogs/GO/go/5_go_operation.html" class="sidebar-link">GO语言基础之运算符</a></li><li><a href="/blogs/GO/go/24_go_fmt.html" class="sidebar-link">Go语言fmt.Printf使用指南</a></li><li><a href="/blogs/GO/go/25_go_flag.html" class="sidebar-link">Go语言fmt.Printf使用指南</a></li><li><a href="/blogs/GO/go/6_go_array.html" class="sidebar-link">Go语言基础之数组</a></li><li><a href="/blogs/GO/go/7_go_slice.html" class="sidebar-link">Go语言基础之切片</a></li><li><a href="/blogs/GO/go/8_go_map.html" class="sidebar-link">Go语言基础之map</a></li><li><a href="/blogs/GO/go/9_go_pointer.html" class="sidebar-link">Go语言基础之指针</a></li><li><a href="/blogs/GO/go/10_go_func.html" class="sidebar-link">Go语言基础之函数</a></li><li><a href="/blogs/GO/go/11_go_struct.html" class="sidebar-link">Go语言基础之结构体</a></li><li><a href="/blogs/GO/go/12_go_interface.html" class="sidebar-link">Go语言基础之接口</a></li><li><a href="/blogs/GO/go/26_go_reflect.html" class="sidebar-link">Go语言基础之反射</a></li><li><a href="/blogs/GO/go/13_go_package.html" class="sidebar-link">Go语言基础之包(package)</a></li><li><a href="/blogs/GO/go/14_go_channel_lock.html" class="sidebar-link">Go语言中的并发编程</a></li><li><a href="/blogs/GO/go/15_go_socket_udp_tcp.html" class="sidebar-link">Go语言基础之网络编程</a></li><li><a href="/blogs/GO/go/16_go_net_http.html" class="sidebar-link">Go语言基础之net/http介绍</a></li><li><a href="/blogs/GO/go/23_go_Context.html" class="sidebar-link">Go标准库Context</a></li><li><a href="/blogs/GO/go/17_go_test.html" aria-current="page" class="active sidebar-link">Go语言基础之单元测试</a></li><li><a href="/blogs/GO/go/18_go_mod.html" class="sidebar-link">Go语言基础之依赖管理</a></li><li><a href="/blogs/GO/go/19_go_time.html" class="sidebar-link">Go语言基础之time包</a></li><li><a href="/blogs/GO/go/20_go_strconv.html" class="sidebar-link">Go语言内置包之strconv</a></li><li><a href="/blogs/GO/go/22_go_http_template_1.html" class="sidebar-link">Go语言标准库之http/template</a></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gin</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>数据库操作</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gorm</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>gorm</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>NoSql</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>消息队列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>go三方库</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>Go语言基础之单元测试</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">Go语言基础之单元测试</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>小狐</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>9/10/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>GO</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="go语言基础之单元测试"><a href="#go语言基础之单元测试" class="header-anchor">#</a> Go语言基础之单元测试</h1> <p>不写测试的开发不是好程序员。我个人非常崇尚TDD（Test Driven Development）的，然而可惜的是国内的程序员都不太关注测试这一部分。 这篇文章主要介绍下在Go语言中如何做单元测试和基准测试。</p> <h1 id="go-test工具"><a href="#go-test工具" class="header-anchor">#</a> go test工具</h1> <p>Go语言中的测试依赖go test命令。编写测试代码和编写普通的Go代码过程是类似的，并不需要学习新的语法、规则或工具。
go test命令是一个按照一定约定和组织的测试代码的驱动程序。在包目录内，所有以_test.go为后缀名的源代码文件都是go test测试的一部分，不会被go build编译到最终的可执行文件中。
在*_test.go文件中有三种类型的函数，单元测试函数、基准测试函数和示例函数。</p> <table><thead><tr><th>类型</th> <th>格式</th> <th>作用</th></tr></thead> <tbody><tr><td>测试函数</td> <td>函数名前缀为Test</td> <td>测试程序的一些逻辑行为是否正确</td></tr> <tr><td>基准函数</td> <td>函数名前缀为Benchmark</td> <td>测试函数的性能</td></tr> <tr><td>示例函数</td> <td>函数名前缀为Example</td> <td>为文档提供示例文档</td></tr></tbody></table> <p>go test命令会遍历所有的*_test.go文件中符合上述命名规则的函数，然后生成一个临时的main包用于调用相应的测试函数，然后构建并运行、报告测试结果，最后清理测试中生成的临时文件。</p> <h1 id="测试函数"><a href="#测试函数" class="header-anchor">#</a> 测试函数</h1> <h2 id="测试函数的格式"><a href="#测试函数的格式" class="header-anchor">#</a> 测试函数的格式</h2> <p>每个测试函数必须导入testing包，测试函数的基本格式（签名）如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestName</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>测试函数的名字必须以Test开头，可选的后缀名必须以大写字母开头，举几个例子：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestAdd</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">TestSum</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">TestLog</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>其中参数t用于报告测试失败和附加的日志信息。 testing.T的拥有的方法如下：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Error</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Errorf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">FailNow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Failed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Fatal</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Fatalf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Log</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Logf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>t <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Parallel</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>t <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Run</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> f <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>T<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Skip</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">SkipNow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Skipf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>T<span class="token punctuation">)</span> <span class="token function">Skipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="测试函数示例"><a href="#测试函数示例" class="header-anchor">#</a> 测试函数示例</h2> <p>就像细胞是构成我们身体的基本单位，一个软件程序也是由很多单元组件构成的。单元组件可以是函数、结构体、方法和最终用户可能依赖的任意东西。总之我们需要确保这些组件是能够正常运行的。单元测试是一些利用各种方法测试单元组件的程序，它会将结果与预期输出进行比较。
接下来，我们定义一个split的包，包中定义了一个Split函数，具体实现如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// split/split.go</span>

<span class="token keyword">package</span> split

<span class="token keyword">import</span> <span class="token string">&quot;strings&quot;</span>

<span class="token comment">// split package with a single split function.</span>

<span class="token comment">// Split slices s into all substrings separated by sep and</span>
<span class="token comment">// returns a slice of the substrings between those separators.</span>
<span class="token keyword">func</span> <span class="token function">Split</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>result <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	i <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>

	<span class="token keyword">for</span> i <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">{</span>
		result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">[</span><span class="token punctuation">:</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
		s <span class="token operator">=</span> s<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
		i <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">)</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>在当前目录下，我们创建一个split_test.go的测试文件，并定义一个测试函数如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// split/split_test.go</span>

<span class="token keyword">package</span> split

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">&quot;reflect&quot;</span>
	<span class="token string">&quot;testing&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 测试函数名必须以Test开头，必须接收一个*testing.T类型参数</span>
	got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">)</span>         <span class="token comment">// 程序输出的结果</span>
	want <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span>    <span class="token comment">// 期望的结果</span>
	<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 因为slice不能比较直接，借助反射包中的方法比较</span>
		t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%v, got:%v&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">,</span> got<span class="token punctuation">)</span> <span class="token comment">// 测试失败输出错误提示</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>此时split这个包中的文件如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ ls <span class="token operator">-</span>l
total <span class="token number">16</span>
<span class="token operator">-</span>rw<span class="token operator">-</span>r<span class="token operator">--</span>r<span class="token operator">--</span>  <span class="token number">1</span> liwenzhou  staff  <span class="token number">408</span>  <span class="token number">4</span> <span class="token number">29</span> <span class="token number">15</span><span class="token punctuation">:</span><span class="token number">50</span> split<span class="token punctuation">.</span><span class="token keyword">go</span>
<span class="token operator">-</span>rw<span class="token operator">-</span>r<span class="token operator">--</span>r<span class="token operator">--</span>  <span class="token number">1</span> liwenzhou  staff  <span class="token number">466</span>  <span class="token number">4</span> <span class="token number">29</span> <span class="token number">16</span><span class="token punctuation">:</span><span class="token number">04</span> split_test<span class="token punctuation">.</span><span class="token keyword">go</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>在split包路径下，执行go test命令，可以看到输出结果如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.005</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>一个测试用例有点单薄，我们再编写一个测试使用多个字符切割字符串的例子，在split_test.go中添加如下测试函数：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestMoreSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">)</span>
	want <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span>
	<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%v, got:%v&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">,</span> got<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>再次运行go test命令，输出结果如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestMultiSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span>a d<span class="token punctuation">]</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span>a cd<span class="token punctuation">]</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>这一次，我们的测试失败了。我们可以为go test命令添加-v参数，查看测试函数名称和运行时间：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestMoreSplit
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestMoreSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">21</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span>a d<span class="token punctuation">]</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span>a cd<span class="token punctuation">]</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.005</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>这一次我们能清楚的看到是TestMoreSplit这个测试没有成功。 还可以在go test命令后添加-run参数，它对应一个正则表达式，只有函数名匹配上的测试函数才会被go test命令执行。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v <span class="token operator">-</span>run<span class="token operator">=</span><span class="token string">&quot;More&quot;</span>
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestMoreSplit
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestMoreSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">21</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span>a d<span class="token punctuation">]</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span>a cd<span class="token punctuation">]</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>现在我们回过头来解决我们程序中的问题。很显然我们最初的split函数并没有考虑到sep为多个字符的情况，我们来修复下这个Bug：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> split

<span class="token keyword">import</span> <span class="token string">&quot;strings&quot;</span>

<span class="token comment">// split package with a single split function.</span>

<span class="token comment">// Split slices s into all substrings separated by sep and</span>
<span class="token comment">// returns a slice of the substrings between those separators.</span>
<span class="token keyword">func</span> <span class="token function">Split</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>result <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	i <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>

	<span class="token keyword">for</span> i <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">{</span>
		result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">[</span><span class="token punctuation">:</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
		s <span class="token operator">=</span> s<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token function">len</span><span class="token punctuation">(</span>sep<span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment">// 这里使用len(sep)获取sep的长度</span>
		i <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">)</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>这一次我们再来测试一下，我们的程序。注意，当我们修改了我们的代码之后不要仅仅执行那些失败的测试函数，我们应该完整的运行所有的测试，保证不会因为修改代码而引入了新的问题。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestMoreSplit
<span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestMoreSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>这一次我们的测试都通过了。</p> <h2 id="测试组"><a href="#测试组" class="header-anchor">#</a> 测试组</h2> <p>我们现在还想要测试一下split函数对中文字符串的支持，这个时候我们可以再编写一个TestChineseSplit测试函数，但是我们也可以使用如下更友好的一种方式来添加更多的测试用例。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token comment">// 定义一个测试用例类型</span>
	<span class="token keyword">type</span> test <span class="token keyword">struct</span> <span class="token punctuation">{</span>
		input <span class="token builtin">string</span>
		sep   <span class="token builtin">string</span>
		want  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 定义一个存储测试用例的切片</span>
	tests <span class="token operator">:=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>test<span class="token punctuation">{</span>
		<span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token comment">// 遍历切片，逐一执行测试用例</span>
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> tc <span class="token operator">:=</span> <span class="token keyword">range</span> tests <span class="token punctuation">{</span>
		got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span>tc<span class="token punctuation">.</span>input<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>sep<span class="token punctuation">)</span>
		<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>got<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%v, got:%v&quot;</span><span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><p>我们通过上面的代码把多个测试用例合到一起，再次执行go test命令。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">42</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span>河有 又有河<span class="token punctuation">]</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span> 河有 又有河<span class="token punctuation">]</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>我们的测试出现了问题，仔细看打印的测试失败提示信息：excepted:[河有 又有河], got:[ 河有 又有河]，你会发现[ 河有 又有河]中有个不明显的空串，这种情况下十分推荐使用%#v的格式化方式。
我们修改下测试用例的格式化输出错误提示部分：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token operator">...</span>
   
	<span class="token keyword">for</span> <span class="token boolean">_</span><span class="token punctuation">,</span> tc <span class="token operator">:=</span> <span class="token keyword">range</span> tests <span class="token punctuation">{</span>
		got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span>tc<span class="token punctuation">.</span>input<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>sep<span class="token punctuation">)</span>
		<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>got<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%#v, got:%#v&quot;</span><span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>此时运行go test命令后就能看到比较明显的提示信息了：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">42</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="子测试"><a href="#子测试" class="header-anchor">#</a> 子测试</h2> <p>看起来都挺不错的，但是如果测试用例比较多的时候，我们是没办法一眼看出来具体是哪个测试用例失败了。我们可能会想到下面的解决办法：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">type</span> test <span class="token keyword">struct</span> <span class="token punctuation">{</span> <span class="token comment">// 定义test结构体</span>
		input <span class="token builtin">string</span>
		sep   <span class="token builtin">string</span>
		want  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
	<span class="token punctuation">}</span>
	tests <span class="token operator">:=</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>test<span class="token punctuation">{</span> <span class="token comment">// 测试用例使用map存储</span>
		<span class="token string">&quot;simple&quot;</span><span class="token punctuation">:</span>      <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;wrong sep&quot;</span><span class="token punctuation">:</span>   <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;more sep&quot;</span><span class="token punctuation">:</span>    <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;leading sep&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> name<span class="token punctuation">,</span> tc <span class="token operator">:=</span> <span class="token keyword">range</span> tests <span class="token punctuation">{</span>
		got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span>tc<span class="token punctuation">.</span>input<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>sep<span class="token punctuation">)</span>
		<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>got<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;name:%s excepted:%#v, got:%#v&quot;</span><span class="token punctuation">,</span> name<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span> <span class="token comment">// 将测试用例的name格式化输出</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>上面的做法是能够解决问题的。同时Go1.7+中新增了子测试，我们可以按照如下方式使用t.Run执行子测试：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">type</span> test <span class="token keyword">struct</span> <span class="token punctuation">{</span> <span class="token comment">// 定义test结构体</span>
		input <span class="token builtin">string</span>
		sep   <span class="token builtin">string</span>
		want  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
	<span class="token punctuation">}</span>
	tests <span class="token operator">:=</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>test<span class="token punctuation">{</span> <span class="token comment">// 测试用例使用map存储</span>
		<span class="token string">&quot;simple&quot;</span><span class="token punctuation">:</span>      <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;wrong sep&quot;</span><span class="token punctuation">:</span>   <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;more sep&quot;</span><span class="token punctuation">:</span>    <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;leading sep&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">for</span> name<span class="token punctuation">,</span> tc <span class="token operator">:=</span> <span class="token keyword">range</span> tests <span class="token punctuation">{</span>
		t<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 使用t.Run()执行子测试</span>
			got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span>tc<span class="token punctuation">.</span>input<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>sep<span class="token punctuation">)</span>
			<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>got<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">)</span> <span class="token punctuation">{</span>
				t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%#v, got:%#v&quot;</span><span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>此时我们再执行go test命令就能够看到更清晰的输出内容了：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>leading_sep
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>simple
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>wrong_sep
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>more_sep
<span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    <span class="token operator">--</span><span class="token operator">-</span> FAIL<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>leading_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">83</span><span class="token punctuation">:</span> excepted<span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">,</span> got<span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span>
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>simple <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>wrong_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>more_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
FAIL
exit status <span class="token number">1</span>
FAIL    github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>这个时候我们要把测试用例中的错误修改回来：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token operator">...</span>
	tests <span class="token operator">:=</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>test<span class="token punctuation">{</span> <span class="token comment">// 测试用例使用map存储</span>
		<span class="token string">&quot;simple&quot;</span><span class="token punctuation">:</span>      <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;wrong sep&quot;</span><span class="token punctuation">:</span>   <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;more sep&quot;</span><span class="token punctuation">:</span>    <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;leading sep&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	<span class="token operator">...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>我们都知道可以通过-run=RegExp来指定运行的测试用例，还可以通过/来指定要运行的子测试用例，例如：go test -v -run=Split/simple只会运行simple对应的子测试用例。</p> <h2 id="测试覆盖率"><a href="#测试覆盖率" class="header-anchor">#</a> 测试覆盖率</h2> <p>测试覆盖率是你的代码被测试套件覆盖的百分比。通常我们使用的都是语句的覆盖率，也就是在测试中至少被运行一次的代码占总代码的比例。
Go提供内置功能来检查你的代码覆盖率。我们可以使用go test -cover来查看测试覆盖率。例如：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>cover
PASS
coverage<span class="token punctuation">:</span> <span class="token number">100.0</span><span class="token operator">%</span> of statements
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.005</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>从上面的结果可以看到我们的测试用例覆盖了100%的代码。
Go还提供了一个额外的-coverprofile参数，用来将覆盖率相关的记录信息输出到一个文件。例如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>cover <span class="token operator">-</span>coverprofile<span class="token operator">=</span>c<span class="token punctuation">.</span>out
PASS
coverage<span class="token punctuation">:</span> <span class="token number">100.0</span><span class="token operator">%</span> of statements
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.005</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面的命令会将覆盖率相关的信息输出到当前文件夹下面的c.out文件中，然后我们执行go tool cover -html=c.out，使用cover工具来处理生成的记录信息，该命令会打开本地的浏览器窗口生成一个HTML报告。上图中每个用绿色标记的语句块表示被覆盖了，而红色的表示没有被覆盖。</p> <h1 id="基准测试"><a href="#基准测试" class="header-anchor">#</a> 基准测试</h1> <h2 id="基准测试函数格式"><a href="#基准测试函数格式" class="header-anchor">#</a> 基准测试函数格式</h2> <p>基准测试就是在一定的工作负载之下检测程序性能的一种方法。基准测试的基本格式如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">BenchmarkName</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>基准测试以Benchmark为前缀，需要一个*testing.B类型的参数b，基准测试必须要执行b.N次，这样的测试才有对照性，b.N的值是系统根据实际情况去调整的，从而保证测试的稳定性。 testing.B拥有的方法如下：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Error</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Errorf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">FailNow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Failed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Fatal</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Fatalf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Log</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Logf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">ReportAllocs</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">ResetTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Run</span><span class="token punctuation">(</span>name <span class="token builtin">string</span><span class="token punctuation">,</span> f <span class="token keyword">func</span><span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">RunParallel</span><span class="token punctuation">(</span>body <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token operator">*</span>PB<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">SetBytes</span><span class="token punctuation">(</span>n <span class="token builtin">int64</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">SetParallelism</span><span class="token punctuation">(</span>p <span class="token builtin">int</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Skip</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">SkipNow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Skipf</span><span class="token punctuation">(</span>format <span class="token builtin">string</span><span class="token punctuation">,</span> args <span class="token operator">...</span><span class="token keyword">interface</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">Skipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">bool</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">StartTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>b <span class="token operator">*</span>B<span class="token punctuation">)</span> <span class="token function">StopTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="基准测试示例"><a href="#基准测试示例" class="header-anchor">#</a> 基准测试示例</h2> <p>我们为split包中的Split函数编写基准测试如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">BenchmarkSplit</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> b<span class="token punctuation">.</span>N<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		<span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>基准测试并不会默认执行，需要增加-bench参数，所以我们通过执行go test -bench=Split命令执行基准测试，输出结果如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span>Split
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split
BenchmarkSplit<span class="token operator">-</span><span class="token number">8</span>        <span class="token number">10000000</span>               <span class="token number">203</span> ns<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">2.255</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>其中BenchmarkSplit-8表示对Split函数进行基准测试，数字8表示GOMAXPROCS的值，这个对于并发基准测试很重要。10000000和203ns/op表示每次调用Split函数耗时203ns，这个结果是10000000次调用的平均值。
我们还可以为基准测试添加-benchmem参数，来获得内存分配的统计数据。</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span>Split <span class="token operator">-</span>benchmem
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split
BenchmarkSplit<span class="token operator">-</span><span class="token number">8</span>        <span class="token number">10000000</span>               <span class="token number">215</span> ns<span class="token operator">/</span>op             <span class="token number">112</span> B<span class="token operator">/</span>op          <span class="token number">3</span> allocs<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">2.394</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>其中，112 B/op表示每次操作内存分配了112字节，3 allocs/op则表示每次操作进行了3次内存分配。 我们将我们的Split函数优化如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">Split</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep <span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>result <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	result <span class="token operator">=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> strings<span class="token punctuation">.</span><span class="token function">Count</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span>
	i <span class="token operator">:=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>
	<span class="token keyword">for</span> i <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">{</span>
		result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">[</span><span class="token punctuation">:</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
		s <span class="token operator">=</span> s<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token function">len</span><span class="token punctuation">(</span>sep<span class="token punctuation">)</span><span class="token punctuation">:</span><span class="token punctuation">]</span> <span class="token comment">// 这里使用len(sep)获取sep的长度</span>
		i <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">Index</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> sep<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
	result <span class="token operator">=</span> <span class="token function">append</span><span class="token punctuation">(</span>result<span class="token punctuation">,</span> s<span class="token punctuation">)</span>
	<span class="token keyword">return</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>这一次我们提前使用make函数将result初始化为一个容量足够大的切片，而不再像之前一样通过调用append函数来追加。我们来看一下这个改进会带来多大的性能提升：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span>Split <span class="token operator">-</span>benchmem
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split
BenchmarkSplit<span class="token operator">-</span><span class="token number">8</span>        <span class="token number">10000000</span>               <span class="token number">127</span> ns<span class="token operator">/</span>op              <span class="token number">48</span> B<span class="token operator">/</span>op          <span class="token number">1</span> allocs<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">1.423</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>这个使用make函数提前分配内存的改动，减少了2/3的内存分配次数，并且减少了一半的内存分配。</p> <h2 id="性能比较函数"><a href="#性能比较函数" class="header-anchor">#</a> 性能比较函数</h2> <p>上面的基准测试只能得到给定操作的绝对耗时，但是在很多性能问题是发生在两个不同操作之间的相对耗时，比如同一个函数处理1000个元素的耗时与处理1万甚至100万个元素的耗时的差别是多少？再或者对于同一个任务究竟使用哪种算法性能最佳？我们通常需要对两个不同算法的实现使用相同的输入来进行基准比较测试。
性能比较函数通常是一个带有参数的函数，被多个不同的Benchmark函数传入不同的值来调用。举个例子如下：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">benchmark</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">,</span> size <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">/* ... */</span><span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">Benchmark10</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">benchmark</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">Benchmark100</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">benchmark</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">Benchmark1000</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token function">benchmark</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>例如我们编写了一个计算斐波那契数列的函数如下：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// fib.go</span>

<span class="token comment">// Fib 是一个计算第n个斐波那契数的函数</span>
<span class="token keyword">func</span> <span class="token function">Fib</span><span class="token punctuation">(</span>n <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token builtin">int</span> <span class="token punctuation">{</span>
	<span class="token keyword">if</span> n <span class="token operator">&lt;</span> <span class="token number">2</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> n
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token function">Fib</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">Fib</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>我们编写的性能比较函数如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// fib_test.go</span>

<span class="token keyword">func</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">,</span> n <span class="token builtin">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> b<span class="token punctuation">.</span>N<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		<span class="token function">Fib</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">BenchmarkFib1</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span>  <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFib2</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span>  <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFib3</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span>  <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFib10</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFib20</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFib40</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">benchmarkFib</span><span class="token punctuation">(</span>b<span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>运行基准测试：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span><span class="token punctuation">.</span>
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>fib
BenchmarkFib1<span class="token operator">-</span><span class="token number">8</span>         <span class="token number">1000000000</span>               <span class="token number">2.03</span> ns<span class="token operator">/</span>op
BenchmarkFib2<span class="token operator">-</span><span class="token number">8</span>         <span class="token number">300000000</span>                <span class="token number">5.39</span> ns<span class="token operator">/</span>op
BenchmarkFib3<span class="token operator">-</span><span class="token number">8</span>         <span class="token number">200000000</span>                <span class="token number">9.71</span> ns<span class="token operator">/</span>op
BenchmarkFib10<span class="token operator">-</span><span class="token number">8</span>         <span class="token number">5000000</span>               <span class="token number">325</span> ns<span class="token operator">/</span>op
BenchmarkFib20<span class="token operator">-</span><span class="token number">8</span>           <span class="token number">30000</span>             <span class="token number">42460</span> ns<span class="token operator">/</span>op
BenchmarkFib40<span class="token operator">-</span><span class="token number">8</span>               <span class="token number">2</span>         <span class="token number">638524980</span> ns<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>fib <span class="token number">12.944</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>这里需要注意的是，默认情况下，每个基准测试至少运行1秒。如果在Benchmark函数返回时没有到1秒，则b.N的值会按1,2,5,10,20,50，…增加，并且函数再次运行。
最终的BenchmarkFib40只运行了两次，每次运行的平均值只有不到一秒。像这种情况下我们应该可以使用-benchtime标志增加最小基准时间，以产生更准确的结果。例如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span>Fib40 <span class="token operator">-</span>benchtime<span class="token operator">=</span><span class="token number">20</span>s
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>fib
BenchmarkFib40<span class="token operator">-</span><span class="token number">8</span>              <span class="token number">50</span>         <span class="token number">663205114</span> ns<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>fib <span class="token number">33.849</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>这一次BenchmarkFib40函数运行了50次，结果就会更准确一些了。
使用性能比较函数做测试的时候一个容易犯的错误就是把b.N作为输入的大小，例如以下两个例子都是错误的示范：
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 错误示范1</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFibWrong</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">for</span> n <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> n <span class="token operator">&lt;</span> b<span class="token punctuation">.</span>N<span class="token punctuation">;</span> n<span class="token operator">++</span> <span class="token punctuation">{</span>
		<span class="token function">Fib</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 错误示范2</span>
<span class="token keyword">func</span> <span class="token function">BenchmarkFibWrong2</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">Fib</span><span class="token punctuation">(</span>b<span class="token punctuation">.</span>N<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="重置时间"><a href="#重置时间" class="header-anchor">#</a> 重置时间</h2> <p>b.ResetTimer之前的处理不会放到执行时间里，也不会输出到报告中，所以可以在之前做一些不计划作为测试报告的操作。例如：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">BenchmarkSplit</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	time<span class="token punctuation">.</span><span class="token function">Sleep</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">*</span> time<span class="token punctuation">.</span>Second<span class="token punctuation">)</span> <span class="token comment">// 假设需要做一些耗时的无关操作</span>
	b<span class="token punctuation">.</span><span class="token function">ResetTimer</span><span class="token punctuation">(</span><span class="token punctuation">)</span>              <span class="token comment">// 重置计时器</span>
	<span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> b<span class="token punctuation">.</span>N<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
		<span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="并行测试"><a href="#并行测试" class="header-anchor">#</a> 并行测试</h2> <p>func (b *B) RunParallel(body func(*PB))会以并行的方式执行给定的基准测试。
RunParallel会创建出多个goroutine，并将b.N分配给这些goroutine执行， 其中goroutine数量的默认值为GOMAXPROCS。用户如果想要增加非CPU受限（non-CPU-bound）基准测试的并行性， 那么可以在RunParallel之前调用SetParallelism 。RunParallel通常会与-cpu标志一同使用。
​</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">BenchmarkSplitParallel</span><span class="token punctuation">(</span>b <span class="token operator">*</span>testing<span class="token punctuation">.</span>B<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">// b.SetParallelism(1) // 设置使用的CPU数</span>
	b<span class="token punctuation">.</span><span class="token function">RunParallel</span><span class="token punctuation">(</span><span class="token keyword">func</span><span class="token punctuation">(</span>pb <span class="token operator">*</span>testing<span class="token punctuation">.</span>PB<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">for</span> pb<span class="token punctuation">.</span><span class="token function">Next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>执行一下基准测试：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>bench<span class="token operator">=</span><span class="token punctuation">.</span>
goos<span class="token punctuation">:</span> darwin
goarch<span class="token punctuation">:</span> amd64
pkg<span class="token punctuation">:</span> github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split
BenchmarkSplit<span class="token operator">-</span><span class="token number">8</span>                <span class="token number">10000000</span>               <span class="token number">131</span> ns<span class="token operator">/</span>op
BenchmarkSplitParallel<span class="token operator">-</span><span class="token number">8</span>        <span class="token number">50000000</span>                <span class="token number">36.1</span> ns<span class="token operator">/</span>op
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">3.308</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>还可以通过在测试命令后添加-cpu参数如go test -bench=. -cpu 1来指定使用的CPU数量。</p> <h1 id="setup与teardown"><a href="#setup与teardown" class="header-anchor">#</a> Setup与TearDown</h1> <p>测试程序有时需要在测试之前进行额外的设置（setup）或在测试之后进行拆卸（teardown）。</p> <h2 id="testmain"><a href="#testmain" class="header-anchor">#</a> TestMain</h2> <p>通过在*_test.go文件中定义TestMain函数来可以在测试之前进行额外的设置（setup）或在测试之后进行拆卸（teardown）操作。
如果测试文件包含函数:func TestMain(m *testing.M)那么生成的测试会先调用 TestMain(m)，然后再运行具体测试。TestMain运行在主goroutine中, 可以在调用 m.Run前后做任何设置（setup）和拆卸（teardown）。退出测试的时候应该使用m.Run的返回值作为参数调用os.Exit。
一个使用TestMain来设置Setup和TearDown的示例如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestMain</span><span class="token punctuation">(</span>m <span class="token operator">*</span>testing<span class="token punctuation">.</span>M<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;write setup code here...&quot;</span><span class="token punctuation">)</span> <span class="token comment">// 测试之前的做一些设置</span>
	<span class="token comment">// 如果 TestMain 使用了 flags，这里应该加上flag.Parse()</span>
	retCode <span class="token operator">:=</span> m<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span>                         <span class="token comment">// 执行测试</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;write teardown code here...&quot;</span><span class="token punctuation">)</span> <span class="token comment">// 测试之后做一些拆卸工作</span>
	os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span>retCode<span class="token punctuation">)</span>                           <span class="token comment">// 退出测试</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>需要注意的是：在调用TestMain时, flag.Parse并没有被调用。所以如果TestMain 依赖于command-line标志 (包括 testing 包的标记), 则应该显示的调用flag.Parse。</p> <h2 id="子测试的setup与teardown"><a href="#子测试的setup与teardown" class="header-anchor">#</a> 子测试的Setup与Teardown</h2> <p>有时候我们可能需要为每个测试集设置Setup与Teardown，也有可能需要为每个子测试设置Setup与Teardown。下面我们定义两个函数工具函数如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token comment">// 测试集的Setup与Teardown</span>
<span class="token keyword">func</span> <span class="token function">setupTestCase</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	t<span class="token punctuation">.</span><span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">&quot;如有需要在此执行:测试之前的setup&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		t<span class="token punctuation">.</span><span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">&quot;如有需要在此执行:测试之后的teardown&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// 子测试的Setup与Teardown</span>
<span class="token keyword">func</span> <span class="token function">setupSubTest</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	t<span class="token punctuation">.</span><span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">&quot;如有需要在此执行:子测试之前的setup&quot;</span><span class="token punctuation">)</span>
	<span class="token keyword">return</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		t<span class="token punctuation">.</span><span class="token function">Log</span><span class="token punctuation">(</span><span class="token string">&quot;如有需要在此执行:子测试之后的teardown&quot;</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>使用方式如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">TestSplit</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">type</span> test <span class="token keyword">struct</span> <span class="token punctuation">{</span> <span class="token comment">// 定义test结构体</span>
		input <span class="token builtin">string</span>
		sep   <span class="token builtin">string</span>
		want  <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span>
	<span class="token punctuation">}</span>
	tests <span class="token operator">:=</span> <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>test<span class="token punctuation">{</span> <span class="token comment">// 测试用例使用map存储</span>
		<span class="token string">&quot;simple&quot;</span><span class="token punctuation">:</span>      <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;wrong sep&quot;</span><span class="token punctuation">:</span>   <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;,&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;more sep&quot;</span><span class="token punctuation">:</span>    <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;abcd&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;bc&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;d&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token string">&quot;leading sep&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>input<span class="token punctuation">:</span> <span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> sep<span class="token punctuation">:</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">,</span> want<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;河有&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;又有河&quot;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">}</span>
	teardownTestCase <span class="token operator">:=</span> <span class="token function">setupTestCase</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span> <span class="token comment">// 测试之前执行setup操作</span>
	<span class="token keyword">defer</span> <span class="token function">teardownTestCase</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span>            <span class="token comment">// 测试之后执行testdoen操作</span>

	<span class="token keyword">for</span> name<span class="token punctuation">,</span> tc <span class="token operator">:=</span> <span class="token keyword">range</span> tests <span class="token punctuation">{</span>
		t<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span>name<span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>t <span class="token operator">*</span>testing<span class="token punctuation">.</span>T<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 使用t.Run()执行子测试</span>
			teardownSubTest <span class="token operator">:=</span> <span class="token function">setupSubTest</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span> <span class="token comment">// 子测试之前执行setup操作</span>
			<span class="token keyword">defer</span> <span class="token function">teardownSubTest</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span>           <span class="token comment">// 测试之后执行testdoen操作</span>
			got <span class="token operator">:=</span> <span class="token function">Split</span><span class="token punctuation">(</span>tc<span class="token punctuation">.</span>input<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>sep<span class="token punctuation">)</span>
			<span class="token keyword">if</span> <span class="token operator">!</span>reflect<span class="token punctuation">.</span><span class="token function">DeepEqual</span><span class="token punctuation">(</span>got<span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">)</span> <span class="token punctuation">{</span>
				t<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;excepted:%#v, got:%#v&quot;</span><span class="token punctuation">,</span> tc<span class="token punctuation">.</span>want<span class="token punctuation">,</span> got<span class="token punctuation">)</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p>测试结果如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code>split $ <span class="token keyword">go</span> test <span class="token operator">-</span>v
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>simple
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>wrong_sep
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>more_sep
<span class="token operator">==</span><span class="token operator">=</span> RUN   TestSplit<span class="token operator">/</span>leading_sep
<span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">71</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>测试之前的setup
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>simple <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">79</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之前的setup
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">81</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之后的teardown
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>wrong_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">79</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之前的setup
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">81</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之后的teardown
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>more_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">79</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之前的setup
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">81</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之后的teardown
    <span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> TestSplit<span class="token operator">/</span>leading_sep <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">79</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之前的setup
        split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">81</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>子测试之后的teardown
    split_test<span class="token punctuation">.</span><span class="token keyword">go</span><span class="token punctuation">:</span><span class="token number">73</span><span class="token punctuation">:</span> 如有需要在此执行<span class="token punctuation">:</span>测试之后的teardown
<span class="token operator">==</span><span class="token operator">=</span> RUN   ExampleSplit
<span class="token operator">--</span><span class="token operator">-</span> PASS<span class="token punctuation">:</span> ExampleSplit <span class="token punctuation">(</span><span class="token number">0.00</span>s<span class="token punctuation">)</span>
PASS
ok      github<span class="token punctuation">.</span>com<span class="token operator">/</span>Q1mi<span class="token operator">/</span>studygo<span class="token operator">/</span>code_demo<span class="token operator">/</span>test_demo<span class="token operator">/</span>split       <span class="token number">0.006</span>s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h1 id="示例函数"><a href="#示例函数" class="header-anchor">#</a> 示例函数</h1> <h2 id="示例函数的格式"><a href="#示例函数的格式" class="header-anchor">#</a> 示例函数的格式</h2> <p>被go test特殊对待的第三种函数就是示例函数，它们的函数名以Example为前缀。它们既没有参数也没有返回值。标准格式如下：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">ExampleName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="示例函数示例"><a href="#示例函数示例" class="header-anchor">#</a> 示例函数示例</h2> <p>下面的代码是我们为Split函数编写的一个示例函数：</p> <div class="language-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">func</span> <span class="token function">ExampleSplit</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>split<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;a:b:c&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;:&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>split<span class="token punctuation">.</span><span class="token function">Split</span><span class="token punctuation">(</span><span class="token string">&quot;沙河有沙又有河&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;沙&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment">// Output:</span>
	<span class="token comment">// [a b c]</span>
	<span class="token comment">// [ 河有 又有河]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>为你的代码编写示例代码有如下三个用处：</p> <ol><li>示例函数能够作为文档直接使用，例如基于web的godoc中能把示例函数与对应的函数或包相关联。</li> <li>示例函数只要包含了// Output:也是可以通过go test运行的可执行测试。split $ go test -run Example PASS ok      github.com/Q1mi/studygo/code_demo/test_demo/split       0.006s</li> <li>示例函数提供了可以直接运行的示例代码，可以直接在golang.org的godoc文档服务器上使用Go Playground运行示例代码。下图为strings.ToUpper函数在Playground的示例函数效果。</li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">2021-9-2: </span> <span class="time">9/3/2021, 6:20:18 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blogs/GO/go/23_go_Context.html" class="prev">
            Go标准库Context
          </a></span> <span class="next"><a href="/blogs/GO/go/18_go_mod.html">
            Go语言基础之依赖管理
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#测试函数的格式" class="sidebar-link reco-side-测试函数的格式" data-v-70334359>测试函数的格式</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#测试函数示例" class="sidebar-link reco-side-测试函数示例" data-v-70334359>测试函数示例</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#测试组" class="sidebar-link reco-side-测试组" data-v-70334359>测试组</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#子测试" class="sidebar-link reco-side-子测试" data-v-70334359>子测试</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#测试覆盖率" class="sidebar-link reco-side-测试覆盖率" data-v-70334359>测试覆盖率</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#基准测试函数格式" class="sidebar-link reco-side-基准测试函数格式" data-v-70334359>基准测试函数格式</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#基准测试示例" class="sidebar-link reco-side-基准测试示例" data-v-70334359>基准测试示例</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#性能比较函数" class="sidebar-link reco-side-性能比较函数" data-v-70334359>性能比较函数</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#重置时间" class="sidebar-link reco-side-重置时间" data-v-70334359>重置时间</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#并行测试" class="sidebar-link reco-side-并行测试" data-v-70334359>并行测试</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#testmain" class="sidebar-link reco-side-testmain" data-v-70334359>TestMain</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#子测试的setup与teardown" class="sidebar-link reco-side-子测试的setup与teardown" data-v-70334359>子测试的Setup与Teardown</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#示例函数的格式" class="sidebar-link reco-side-示例函数的格式" data-v-70334359>示例函数的格式</a></li><li class="level-2" data-v-70334359><a href="/blogs/GO/go/17_go_test.html#示例函数示例" class="sidebar-link reco-side-示例函数示例" data-v-70334359>示例函数示例</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.7029ddab.js" defer></script><script src="/assets/js/3.ebaa3085.js" defer></script><script src="/assets/js/1.8ce67e8c.js" defer></script><script src="/assets/js/18.fbd494c2.js" defer></script>
  </body>
</html>
